//
//  KAButton.swift
//  kano
//
//  Created by Frank on 2024/9/19.
//

import UIKit
import SnapKit

class KAButton: UIButton {
    
    // Properties
    private let iconNameNor: String
    private let iconNameSel: String
    private let text: String
    private let textColorNor: UIColor
    private let textColorSel: UIColor
    private let myFont: UIFont
    private let isAnimated: Bool
    
    private let separateLine = UIView()
    private let iconView = UIImageView()
    private let textLabel = UILabel()
    
    // Computed property to manage selection state
    var hasSelected: Bool = false {
        didSet {
            updateAppearance()
        }
    }
    
    // Initializer
    init(frame: CGRect,
         iconNameNor: String = "",
         iconNameSel: String = "",
         text: String = "",
         textColorNor: UIColor = .clear,
         textColorSel: UIColor = .clear,
         font: UIFont = kFontSize(12),
         isAnimated: Bool = false) {
        self.iconNameNor = iconNameNor
        self.iconNameSel = iconNameSel
        self.text = text
        self.textColorNor = textColorNor
        self.textColorSel = textColorSel
        self.myFont = font
        self.isAnimated = isAnimated
        super.init(frame: frame)
        setupSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // Setup subviews and constraints
    private func setupSubviews() {
        addSubview(separateLine)
        addSubview(iconView)
        addSubview(textLabel)
        
        separateLine.backgroundColor = .clear
        
        // Constraints using SnapKit
        separateLine.snp.makeConstraints { make in
            make.width.equalTo(1)
            make.centerX.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        iconView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(separateLine.snp.left).offset(-4)
            make.width.height.equalTo(14)
        }
        
        textLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(separateLine.snp.right).offset(4)
        }
        
        // Initial appearance setup
        updateAppearance()
    }
    
    // Update appearance based on selection state
    private func updateAppearance() {
        let iconName = hasSelected ? iconNameSel : iconNameNor
        let color = hasSelected ? textColorSel : textColorNor
        
        if isAnimated {
            UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .layoutSubviews, animations: {
                UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
                    self.iconView.transform = CGAffineTransform(scaleX: 1.6, y: 1.6)
                }
                UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
                    self.iconView.transform = .identity
                }
            })
        }
        
        iconView.image = UIImage(named: iconName)
        textLabel.text = text
        textLabel.textColor = color
        textLabel.font = myFont
        textLabel.textAlignment = .center
    }
}
